﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.IO;

using Bierdopje.API;

namespace Sub
{
    public partial class SubDownloader : Form
    {
        private ApiLibrary _API;

        public SubDownloader()
        {
            InitializeComponent();

            _API = new ApiLibrary(Sub.Properties.Settings.Default.ApiKey);
        }

        public List<ApiShow> _Shows;

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Enabled = false;
            try
            {
                if (!tbSearchText.Text.Equals(""))
                {
                    try
                    {
                        _Shows = _API.FindShowByName(tbSearchText.Text);
                        apiShowBindingSource.DataSource = _Shows;
                    }
                    catch (EApiBierdopjeException exception)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show("Error code: " + exception.ErrorCode.ToString() + " - " + exception.ErrorMessage, "Error with API", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (EApiCallNoResults)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show("No results found", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (EApiParseException)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show("There was a problem parsing the request", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (EApiSubsNotPresent)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show("No subtitles present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (EApiException)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show("General API error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (Exception exception)
                    {
                        apiShowBindingSource.DataSource = null;
                        _Shows = null;

                        MessageBox.Show(exception.ToString() + " - " + exception.Message, "General error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                btnSearch.Enabled = true;
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (apiShowBindingSource.DataSource != null)
            {
                if (apiShowBindingSource.Current != null)
                {
                    Wizard _W = new Wizard((ApiShow)apiShowBindingSource.Current);
                    _W.ShowDialog();
                }
            }
        }
    }
}
